package com.aier.service.purchase;

import com.aier.bean.common.CommonResult;
import com.aier.bean.common.GlobalCode;
import com.aier.bean.common.Status;
import com.aier.bean.purchase.PurchaseBean;
import com.aier.exception.CastException;
import com.aier.mapper.purchase.PurchaseMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 采购表service
 */
@Service
public class PurchaseService {

    @Resource
    private PurchaseMapper purchaseMapper;

    /**
     * 获取所有采购信息，状态为01（进行中）列表
     * @return
     */
    public List<PurchaseBean> getAllPurchase(){
        List<PurchaseBean> purchaseBeanList = new ArrayList<>();
        try {
            purchaseBeanList =  purchaseMapper.getAllPurchase(Status.DOING);
        }catch (Exception e){
            e.printStackTrace();
            CastException.cast(GlobalCode.GET_ALL_PURCHASE_FAIL);
        }
        return purchaseBeanList;
    }

    /**
     * 插入采购信息
     * @param purchaseBean
     */
    public CommonResult savePurchase(PurchaseBean purchaseBean){
        try {
            purchaseMapper.insert(purchaseBean);
        }catch (Exception e){
            e.printStackTrace();
            CastException.cast(GlobalCode.SAVE_PURCHASE_FAIL);
        }
        return CommonResult.success();
    }

    /**
     * 获取所有ContractCode列表
     * @return
     */
    public CommonResult getAllContractCode(){
        List<String> modeNameList = purchaseMapper.getAllContractCode(Status.DOING);
        return CommonResult.success(modeNameList);
    }

    /**
     * 判断contractCode是否已经记录,存在则抛异常
     * @param purchaseBean
     */
    public void contractCodeIsExist(PurchaseBean purchaseBean){
        List<PurchaseBean> purchaseBeanList = purchaseMapper.getAllByContractCode(purchaseBean.getContractCode(),Status.DOING);
        if (purchaseBeanList.size()>0){
            CastException.cast(GlobalCode.CONTRACT_CODE_IS_EXIST);
        }
    }


    /**
     * 判断contractCode是否存在采购表,不存在则抛异常
     * @param contractCode
     */
    public void contractCodeNotExist(String contractCode){
        List<PurchaseBean> purchaseBeanList = purchaseMapper.getAllByContractCode(contractCode,Status.DOING);
        if (purchaseBeanList.size() == 0){
            CastException.cast(GlobalCode.CONTRACT_CODE_NOT_EXIST);
        }
    }

    /**
     * 判断contractCode,modelName是否存在，不存在则抛异常
     * @param contractCode
     * @param modelName
     */
    public void isExistByContractCodeAndModelName(String contractCode, String modelName){
        List<PurchaseBean> purchaseBeanList = purchaseMapper.getListByContractCodeAndModelName(contractCode, modelName,Status.DOING);
        if (purchaseBeanList.size() == 0){
            CastException.cast(GlobalCode.CONTRACT_CODE_AND_MODEL_NAME_NOT_EXIST);
        }
    }


    /**
     * 根据contractCode获取modelname
     * @param contractCode
     * @return
     */
    public CommonResult getModeNameByContractCode(String contractCode) {
        List<String> modeNameList = purchaseMapper.getModelNameByContractCode(contractCode,Status.DOING);
        return CommonResult.success(modeNameList);
    }

    /**
     * 结束合同（订单完成,状态改为已完成）
     * @param contractCode
     */
    public void endContractCode(String contractCode){
        purchaseMapper.endContractCode(contractCode, Status.COMPLETED);
    }
}
